#include <iostream>
#include <utility>
#include <thread>
#include <chrono>

void f1(int n){
    for (int i = 0; i < 2; ++i) {
        std::cout << "Thread 1 executing\n";
        ++n;
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }
}

void f2(int& n){
    for (int i = 0; i < 2; ++i) {
        std::cout << "Thread 2 executing\n";
        ++n;
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }
}

class foo{
public:
    void bar(){
        for (int i = 0; i < 2; ++i) {
            std::cout << "Thread 3 executing\n";
            ++n;
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
        }
    }
    int n = 0;
};

class baz{
public:
    void operator()(){
        int n = 0;
        for (int i = 0; i < 2; ++i) {
            std::cout << "Thread 4 executing\n";
            ++n;
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
        }
    }
};

int main(){
    int n = 0;
    foo f;
    baz b;
    std::thread t1; // t1 不是线程
    std::thread t2(f1, n + 1); // 按值传递
    std::thread t3(f2, std::ref(n)); // 按引用传递
    std::thread t4(std::move(t3)); // t4 现在运行 f2() 。 t3 不再是线程
    std::thread t5(&foo::bar, &f); // t5 在对象 f 上运行 foo::bar()
    std::thread t6(b); // t6 在对象 b 上运行 baz::operator()

    t1 = std::thread(f1, n + 1); //t1等号运算符重载
    std::thread t7(std::thread(f2, std::ref(n))); //t7拷贝构造
    t2.join();
    t4.join();
    t5.join();
    t6.join();
    t1.join();
    t7.join();
    std::cout << "Final value of n is " << n << '\n';
    std::cout << "Final value of foo::n is " << f.n << '\n';
}
